package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.adapter.CarListAdapter;
import com.fqcar.fqdf.adapter.SearchCarListAdapter;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.entity.CarDetailEntity;
import com.fqcar.fqdf.entity.CarListDetailEntity;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.DialogUtil;
import com.fqdf.framework.android.net.response.NetBeanWrapper;
import com.fqdf.framework.android.util.CheckLogicUtil;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by lvsidong on 2017/10/30.
 * <p>
 * 查找车辆
 */

public class SearchCarActivity extends BaseRequestActivity{
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_tv)
    TextView searchTv;
    @BindView(R.id.search_list)
    ListView searchList;

//    private SearchCarAdapter searchCarAdapter;

    private CarListAdapter searchCarAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        initMyView();
    }

    private void initMyView() {
        searchCarAdapter = new CarListAdapter(this);
        searchList.setAdapter(searchCarAdapter);
    }

    private void requestSearchResult(String carNum) {
        getProgressDialog().show();
        RequestOptService.getSearchCarList(getApplicationContext(), carNum, new Response.Listener<NetBeanWrapper<CarListDetailEntity>>() {
            @Override
            public void onResponse(NetBeanWrapper<CarListDetailEntity> response) {
                getProgressDialog().dismiss();
                if (handleBeanResponse(response)) {
                    return;
                }
                CarListDetailEntity carListDetailEntity = response.getModelData();

                searchCarAdapter.setList(carListDetailEntity.getCarList());
                searchCarAdapter.notifyDataSetChanged();

            }
        }, getDefaultErrorListener());
    }

    @OnItemClick(R.id.search_list)
    public void onItemClick(int position) {
        CarDetailEntity carDetailEntity = searchCarAdapter.getItem(position);
        Intent controlIntent = new Intent(SearchCarActivity.this, StartDealActivity.class);
        controlIntent.putExtra(Constants.BUNDLE_DATA, carDetailEntity.getCarId());
        startActivity(controlIntent);
    }

    @OnClick({R.id.search_back_iv, R.id.search_tv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_back_iv:
                finish();
                break;
            case R.id.search_tv:
                if (!CheckLogicUtil.isEmpty(searchEt.getText().toString())) {
                    requestSearchResult(searchEt.getText().toString());
                } else {
                    DialogUtil.ToastMessage(SearchCarActivity.this, "输入不能为空");
                }
                break;
        }
    }

    /*@Override
    public void onItemClick(View view, int position) {
        CarDetailEntity carDetailEntity = searchCarAdapter.getItem(position);
        Intent controlIntent = new Intent(SearchCarActivity.this, StartDealActivity.class);
        controlIntent.putExtra(Constants.BUNDLE_DATA, carDetailEntity.getCarId());
        startActivity(controlIntent);
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }*/
}
